<template>
    <div>
        <div class="seekersInfo" v-show="isSeekersFormNotNull">
            <el-form :model="seekersForm" label-width="80px" size="medium" style="margin-left:300px">
                <el-form-item label="姓名" style="width:600px">
                    <el-input v-model="seekersForm.full_name" :disabled="ableForInput"></el-input>
                </el-form-item>
                <el-form-item label="性别" style="width:600px">
                    <el-input v-model="seekersForm.gender" :disabled="ableForInput"></el-input>
                </el-form-item>
                <el-form-item label="年龄" style="width:600px">
                    <el-input v-model="seekersForm.age" :disabled="ableForInput"></el-input>
                </el-form-item>
                <el-form-item label="毕业院校" style="width:600px">
                    <el-input v-model="seekersForm.university_one_is_graduated_from" :disabled="ableForInput"></el-input>
                </el-form-item>
                <el-form-item label="毕业年度">
                    <el-col :span="11">
                        <span v-text="seekersForm.graduation_year"></span>
                        <el-date-picker type="month" placeholder="选择年份" :disabled="ableForInput"
                            v-model="seekersForm.graduation_year" format="yyyy 年 MM 月 " value-format="yyyy-MM"
                            style="width: 100%;"></el-date-picker>
                    </el-col>
                    <el-col class="line" :span="2">-</el-col>
                </el-form-item>
                <el-form-item label="学历" style="width:600px">
                    <el-input v-model="seekersForm.education" :disabled="ableForInput"></el-input>
                </el-form-item>
                <el-form-item label="期望职位" style="width:600px">
                    <el-input v-model="seekersForm.expected_position" :disabled="ableForInput"></el-input>
                </el-form-item>
            </el-form>
            <el-button type="primary" v-show="isUpdate" @click="backDoNotUpdate">返回</el-button>
            <el-button type="primary" @click="updateSeekersInfo" v-show="!isAdd">修改</el-button>
            <el-button type="primary" @click="submitSeekersInfo" v-show="isUpdate">提交修改</el-button>
            <el-button type="primary" @click="doAddSeekersInfo" v-show="isAdd">提交</el-button>
        </div>
        <span v-show="doAddItem" class="seekersFormNull">你的求职信息为空<el-button type="primary"
                @click="addSeekersInfo">去完善</el-button></span>
    </div>
</template>
<script>
import axios from 'axios';
export default {
    data() {
        return {
            isAdd: false,
            doAddItem: true,
            isSeekersFormNotNull: false,
            isUpdate: false,
            ableForInput: true,
            seekersForm: {
                job_seekers_id: '',
                full_name: '',
                gender: '',
                age: '',
                university_one_is_graduated_from: '',
                graduation_year: '',
                education: '',
                expected_position: '',
                user_id: window.localStorage.getItem("user_id"),
            }
        };
    },
    methods: {
        addSeekersInfo() {
            this.isSeekersFormNotNull = true;
            this.doAddItem = false;
            this.isAdd = true;
            this.ableForInput = false;
        },
        doAddSeekersInfo() {
            let that = this;
            axios({
                method: 'post',
                url: 'http://127.0.0.1:5000/api/job_seekers/add',
                data: {
                    full_name: that.seekersForm.full_name,
                    gender: that.seekersForm.gender,
                    age: that.seekersForm.age,
                    university_one_is_graduated_from: that.seekersForm.university_one_is_graduated_from,
                    graduation_year: that.seekersForm.graduation_year,
                    user_id: that.seekersForm.user_id,
                    education: that.seekersForm.education,
                    expected_position: that.seekersForm.expected_position,
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    window.location.reload();
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
        updateSeekersInfo() {
            this.isUpdate = true;
            this.ableForInput = false;
        },
        backDoNotUpdate() {
            window.location.reload();
        },
        submitSeekersInfo() {
            let that = this;
            axios({
                method: 'post',
                url: 'http://127.0.0.1:5000/api/job_seekers/set?user_id=' + that.seekersForm.user_id,
                data: {
                    full_name: that.seekersForm.full_name,
                    gender: that.seekersForm.gender,
                    age: that.seekersForm.age,
                    university_one_is_graduated_from: that.seekersForm.university_one_is_graduated_from,
                    graduation_year: that.seekersForm.graduation_year,
                    education: that.seekersForm.education,
                    expected_position: that.seekersForm.expected_position,
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    that.$message.success("更新信息成功");
                    window.location.reload();
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        }
    },
    created: function () {
        if (!window.localStorage.getItem("token")) {
            this.$router.push({ path: '/login' }, () => { }, () => { });
            return;
        }
        let that = this;
        axios({
            method: 'get',
            url: 'http://127.0.0.1:5000/api/job_seekers/get_obj',
            params: {
                user_id: that.seekersForm.user_id,
            },
            headers: {
                'X-Auth-Token': window.localStorage.getItem("token")
            },
        })
            .then(function (response) {
                console.log(response.data);
                if (response.data.result != null) {
                    that.isSeekersFormNotNull = true;
                    that.doAddItem = false;
                    that.seekersForm = response.data.result.obj;
                }
            })
            .catch(function (error) {
                console.log(error);
            })
            .then(function () {
                // 总是会执行
            })
    }
};
</script>
<style lang="less" scoped>
.seekersInfo {
    margin-top: -250px;
    margin-left: 500px;
}
</style>